package cn.iocoder.yudao.module.enterprise.service.model;

import cn.iocoder.yudao.module.enterprise.controller.admin.model.vo.ModelEmissionCreateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.model.ModelConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.model.ModelEmissionDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.model.ModelEmissionMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.MODEL_EMISSION_NOT_EXISTS;

/**
 * 核算模型和排放源关联 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class ModelEmissionServiceImpl implements ModelEmissionService {

    @Resource
    private ModelEmissionMapper modelEmissionMapper;

    @Override
    public Long createModelEmission(ModelEmissionCreateReqVO createReqVO) {
        ModelEmissionDO modelEmissionDO = ModelConvert.INSTANCE.convert(createReqVO);
        modelEmissionMapper.insert(modelEmissionDO);
        return modelEmissionDO.getId();
    }

    @Override
    public void deleteModelEmission(Long id) {
        // 校验存在
        validateModelEmissionExists(id);
        // 删除
        modelEmissionMapper.deleteById(id);
    }

    private void validateModelEmissionExists(Long id) {
        if (modelEmissionMapper.selectById(id) == null) {
            throw exception(MODEL_EMISSION_NOT_EXISTS);
        }
    }

    @Override
    public ModelEmissionDO getModelEmission(Long id) {
        return modelEmissionMapper.selectById(id);
    }

}
